package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.file.service.impl.FileStorageServiceImpl;
import com.zhentao.mapper.WmMaterialMapper;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.wemedia.dto.WmMaterialDto;
import com.zhentao.model.wemedia.pojo.WmMaterial;
import com.zhentao.service.WmMaterialService;
import com.zhentao.utils.common.WmUserThreadLocalUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Service
public class WmMaterialServiceImpl implements WmMaterialService{

    @Autowired
    private FileStorageServiceImpl fileStorageService;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    // 上传素材
    @Override
    @SneakyThrows
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
       // 1校验参数
        if (multipartFile == null || multipartFile.getSize()<=0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2把文件上传到minio
        String url = fileStorageService.uploadImageFile(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
        // 3保存素材信息到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection(0);   //

        wmMaterial.setUserId(WmUserThreadLocalUtil.getUser().getId());
//        System.out.println("拿到id----"+WmUserThreadLocalUtil.getUser().getId());
//        System.out.println("拿到用户----"+WmUserThreadLocalUtil.getUser());

        wmMaterial.setType(0);
        wmMaterial.setUrl(url);
        wmMaterialMapper.insert(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        // 检查参数
        dto.checkParam();

        // 分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        QueryWrapper<WmMaterial> queryWrapper = new QueryWrapper<>();
        // 根据用户id 查询
        queryWrapper.lambda().eq(WmMaterial::getUserId,WmUserThreadLocalUtil.getUser().getId());
        queryWrapper.orderByDesc("id");

        // 是否收藏  // 如果收藏的字段不为空，判断是否收藏
        queryWrapper.lambda().eq(dto.getIsCollection()!=null,WmMaterial::getIsCollection,dto.getIsCollection());

        // 执行查询
        page = wmMaterialMapper.selectPage(page,queryWrapper);

        // 返回结果
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setSize(Integer.valueOf(page.getSize()+""));
        pageResponseResult.setTotal(Integer.valueOf(page.getTotal()+""));
        pageResponseResult.setCurrentPage(Integer.valueOf(page.getCurrent()+""));
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");

        return pageResponseResult;
    }
}